import {ADD_COUNTER,ADD_TO_CART} from './mutations-types'

export default{
  /* 城市显示相关的 */
  changeCity (state, city) {
    state.city = city
    try {
      localStorage.setItem('city', city)
    } catch (e) {}
  },

  /* 如果购物车有该商品就数量++ */
  [ADD_COUNTER](state,payLoad) {
    payLoad.count++
  },

  /* 如果购物车没有该商品就给商品添加count属性和checked属性 */
  [ADD_TO_CART](state,payLoad) {
    payLoad.checked = true
    state.cartList.push(payLoad)
  },

  /* 控制编辑是否显示 */
  rightShow (state) {
    state.rightShow = !state.rightShow
  },

  /* 点击编辑按钮进行商品数量的减 */
  reduce (state,index) {
    if(state.cartList[index].count > 0){
      state.cartList[index].count--
    }
  },
  /* 点击编辑按钮进行商品数量的加 */
  add (state,index) {
    state.cartList[index].count++
  }, 

  deleteClick (state) {
    return state.cartList.forEach((item,index) => {
      if(item.checked === true){
        state.cartList.splice(index,1)
      }
    })
  }
}
